/**
 * 
 */
package com.auth.action;

import java.util.List;

import org.apache.log4j.Logger;


import com.action.AbstractAction;
import com.auth.model.Account;
import com.auth.model.Role;
import com.auth.service.AccountService;
import com.auth.service.AccountServiceImpl;
import com.util.MenuUtil;


public class LoginAction extends AbstractAction 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private AccountService service;
	private Logger logger;
	
	public LoginAction()
	{
		logger = Logger.getLogger(this.getClass());
		service = new AccountServiceImpl();
	}

	private String id;
	private String password;
	private String verifyCode;

	private Account account;
	private Role    role;

	private List<MenuUtil.Menu> menus;
	
	public String execute ()
	{
		return INPUT;
	}
	
	public String init()
	{
		logger.debug("init");
		setSession("account", null);
		return INPUT;
	}
	
	public String login()
	{
		logger.debug("validate_account");

		account = (Account)getSession("account");
		if (account == null)
		{
			if (id == null || id.length() == 0 
					|| password == null || password.length() == 0)
			{
				addActionError("账号密码错误！");
				return INPUT;
			}
	
			if(verifyCode == null || getSession("verifyCode")==null 
					||verifyCode.length()==0 
					|| !verifyCode.toLowerCase().equals(getSession("verifyCode").toString().toLowerCase()))
			{
				addActionError("验证码错误！");
				return INPUT;
			}		
		}
		else
		{
			id = account.getId();
			password = account.getPassword();
		}
		
		account = service.getAccount(id, password);
		if (account == null)
		{
			addActionError("账号或密码错误！");
			return INPUT;			
		}		
		setRole(service.getRole(account.getRoleId()));
		session.setAttribute("account", account);
		session.setAttribute("role", role);
		setMenus(MenuUtil.getInstance().getMenu());
		return SUCCESS;
	}
	
	public String logout()
	{
		logger.debug("logout");
		session.setAttribute("account", null);
		session.setAttribute("role", null);
		return INPUT;
	}

	public List<MenuUtil.Menu> getMenus() {
		return menus;
	}

	public void setMenus(List<MenuUtil.Menu> menus) {
		this.menus = menus;
	}

	/**
	 * @return the account
	 */
	public Account getAccount() {
		return account;
	}

	/**
	 * @param account the account to set
	 */
	public void setAccount(Account account) {
		this.account = account;
	}

	/**
	 * @return the role
	 */
	public Role getRole() {
		return role;
	}

	/**
	 * @param role the role to set
	 */
	public void setRole(Role role) {
		this.role = role;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the verifyCode
	 */
	public String getVerifyCode() {
		return verifyCode;
	}

	/**
	 * @param verifyCode the verifyCode to set
	 */
	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

}
